import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class test23 {


    private static Object List;

    public static void main(String[] args) throws IOException {
        List<Animal> list = get();
    }
    public static List<Animal> get() throws IOException {
        List<Animal> list = new ArrayList<>();

        List<String> ss = FileUtils.readLines(new File("dataAnimal.txt"), "UTF-8");
        for (String s : ss) {
            String[] s1 = s.split(" ");
            try {

                Class<?> aClass = Class.forName(s1[0]);

                Object o = aClass.newInstance();

                Field id = aClass.getField("id");
                id.set(o, s1[1].split("=")[1]);
                Field name = aClass.getField("name");
                name.set(o, s1[2].split("=")[1]);
                if (o instanceof Dog) {
                    Field age = aClass.getDeclaredField("age");
                    age.setAccessible(true);
                    age.set(o, Integer.parseInt(s1[3].split("=")[1]));
                } else if (o instanceof Cat) {
                    Field color = aClass.getDeclaredField("color");
                    color.setAccessible(true);
                    color.set(o, s1[3].split("=")[1]);
                }
                list.add((Animal) o);

            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }


        }

        return (java.util.List<Animal>) List;
    }


}
